<template>
  <div class="container">
    <el-dialog
      :title="form.id ? '更新文章' : '新增文章'"
      :visible="adddialogVisible"
      @close="closeDialog"
    >
      <el-form label-width="120px" :model="form" ref="formRef" :rules="rules">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入文章标题"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
          <quill-editor
            ref="myLQuillEditor"
            v-model="form.articleBody"
            :options="editorOption"
          ></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址">
          <el-input v-model="form.videoURL" placeholder="请输入视频地址"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="addArticle">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, detail, update } from '../../api/hmmm/articles'
export default {
  name: 'Articles-add',
  props: {
    adddialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      // 富文本框配置
      editorOption: {
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }], // toggled buttons
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        },
        placeholder: '请输入正文'
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
          { min: 5, max: 15000, message: '长度在 5 到 15000 个字符', trigger: ['blur', 'change'] }
        ]
      }

    }
  },
  methods: {
    closeDialog () {
      this.$emit('closeDialog')
      this.resetForm()
    },
    resetForm () {
      this.$refs.formRef.resetFields()
      this.form = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
    },
    // 新增
    addArticle () {
      this.$refs.formRef.validate(async valid => {
        // console.log(this.form)
        if (!valid) return

        if (this.form.id) {
          await update(this.form)
        } else {
          await add(this.form)
          this.$emit('updateOne')
        }

        this.$message.success(`${this.form.id ? '更新文章成功' : '新增文章成功'}`)
        this.closeDialog()
        this.$emit('getList')
        this.resetForm()
      })
    },
    // 数据回填
    async getArticleDetail (id) {
      const res = await detail({ id })
      // console.log(res)
      this.form = res.data
      // console.log(this.form)
    }
  }
}
</script>

<style scoped lang='less'>
/deep/ .ql-editor {
  min-height: 200px;
}
</style>
